function prettymat(X,separatorString,precision,outputFile,floatFormat)
% function prettymat(X,separatorString,precision,outputFile,floatFormat)
%
%  print matrix of arbitrary number of columns as one big pretty
%    matrixy looking thing
%  inputs: big matrix of ugly numbers,
%          number of decimals to display
%          string to separate each column of pretty numbers
%              if string is a cell array with 2 elements, then second element will be 
%              printed at end of line, and the first element will be printed between all
%              other output.  if string is a cell array with the same number of elements
%              as X has columns, a different string will separate each column.
%              if string is just a string, string will be printed after every column
%          name of output file [optional]
%          format to print floating point numbers (either 'f' or 'g')
%          
%  defaults: 4 digit precision
%            two space separator string
%            prints to screen
%            prints floats using 'f'
% 
%  written 2/18/1999 by Robert Michaud for Pedro Santa-Clara
%  modified 8/12/1999 by Robert Michaud for Amit Goyal
%  modified 10/19/1999 for Bing Han and Amit Goyal to use different separators
%  to do:  allow for variable precision in either %g or %f format
%          check if file exists before writing

if nargin < 2
   separatorString='  ';
elseif isempty(separatorString)
   separatorString='  ';
end
if nargin < 3
   precision=4;
elseif isempty(precision)
   precision=4;
end
fid=1;
if nargin > 3
   if exist(outputFile)==2
      if menu('\nfile exist!  overwrite?','Yes','No')==2
         return
      end
   end
   if isstr(outputFile)
      fid=fopen(outputFile,'wt');
   end
   if fid==-1
      fprintf('\nerror: can''t write file')
      return
   end
end
if nargin < 5
   floatFormat = 'f';
end

% 
if iscell(separatorString)
   a=(length(separatorString)    -   1 )/(size(X,2)-1);
   b=(size(X,2)-length(separatorString))/(size(X,2)-1);
   for i=1:size(X,2)
      SS{i}=separatorString{floor(a*i + b)};
   end
else
   for i=1:size(X,2)
      SS{i}=separatorString;
   end
end
% initialize P
P(size(X,1),size(X,2)).string=' ';
P(size(X,1),size(X,2)).lengthL=0;
P(size(X,1),size(X,2)).lengthR=0;

% set string to allow for precision
sprintfCommandFloat=sprintf('%%.%d%s',precision,floatFormat);
sprintfCommandInteger=sprintf('%%d');

for i=1:size(X,1)
   for j=1:size(X,2)
      if floor(X(i,j))==X(i,j)
         P(i,j).string=sprintf(sprintfCommandInteger,X(i,j));
      else
         P(i,j).string=sprintf(sprintfCommandFloat,X(i,j));
      end
      % count length to the left and the right of the decimal point
      index=find(P(i,j).string=='.');
      stringLength=length(P(i,j).string);
      if isempty(index)
         index=stringLength+1;
      end
      P(i,j).lengthR=stringLength - index;
      P(i,j).lengthL=index - 1;
   end
end
sizeL=max(reshape([P.lengthL],size(X)),[],1);
sizeR=max(reshape([P.lengthR],size(X)),[],1);

for i=1:size(X,1)
   S=sprintf('\n');
   for j=size(X,2):-1:1
      % confusing, but first pad to the left, then print number, then pad to right, then print separator
      S=[sprintf(sprintf('%%%ds%%s%%%ds%%s',sizeL(j)-P(i,j).lengthL,sizeR(j)-P(i,j).lengthR),...
                         '',P(i,j).string,'',SS{j}) S];
   end
   fprintf(fid,'%s',S);
end

if fid~=1
   fclose(fid);
end
